/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.workload;

import java.util.ArrayList;
import java.util.List;

import org.nudt.jCloud.fileSystem.FileSystem;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.util.ExponentialDistribution;

public class BatchWLfromFileGen implements WorkloadGenerator{
	private FileSystem fileSystem;
	private double avgJobInterarrivalTime;
	private double avgTaskDuration;
	private SlotType slotType;
	
	public BatchWLfromFileGen(FileSystem fileSystem, double avgJobInterarrivalTime, double avgTaskDuration, SlotType slotType){
		setFileSystem(fileSystem);
		setAvgJobInterarrivalTime(avgJobInterarrivalTime);
		setAvgTaskDuration(avgTaskDuration);
		setSlotType(slotType);
	}

	@Override
	public Workload generate(double startTime) {
		Workload batchWorkload = new Workload();
		ExponentialDistribution jobArrivalTimeGen = new ExponentialDistribution(avgJobInterarrivalTime);
		ExponentialDistribution avgTaskDurationGen = new ExponentialDistribution(avgTaskDuration);
		double arrivalTime = startTime;
		for(int i=0; i<fileSystem.getFileList().size(); ++i){
			arrivalTime += jobArrivalTimeGen.sample();
			List<Task> taskList = new ArrayList<Task>();
			double duration = avgTaskDurationGen.sample();
			for(int j=0; j<fileSystem.getFileList().get(i).getBlockList().size(); ++j){
				Task task = new Task(j, duration);
				task.setBlock(fileSystem.getFileList().get(i).getBlockList().get(j));
				taskList.add(task);
			}
			LogicalJob andJob = new AllJob(arrivalTime, batchWorkload);
			andJob.add(new BatchJob(arrivalTime, taskList, slotType, andJob));
			batchWorkload.add(andJob);
		}
		return batchWorkload;
	}

	public FileSystem getFileSystem() {
		return fileSystem;
	}

	public void setFileSystem(FileSystem fileSystem) {
		this.fileSystem = fileSystem;
	}

	public double getAvgJobInterarrivalTime() {
		return avgJobInterarrivalTime;
	}

	public void setAvgJobInterarrivalTime(double avgJobInterarrivalTime) {
		this.avgJobInterarrivalTime = avgJobInterarrivalTime;
	}

	public double getAvgTaskDuration() {
		return avgTaskDuration;
	}

	public void setAvgTaskDuration(double avgTaskDuration) {
		this.avgTaskDuration = avgTaskDuration;
	}
	
	public SlotType getSlotType(){
		return slotType;
	}
	
	public void setSlotType(SlotType slotType){
		this.slotType = slotType;
	}

}
